package com.wmx.thread.pools;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPools {
    static  boolean  flg = true;
    public static void main(String[] args) {

        ThreadPools object = new ThreadPools();
       // ExecutorService pools = Executors.newSingleThreadExecutor();
       ExecutorService pools = Executors.newFixedThreadPool(8);
      /*  pools.execute(() -> {
            System.out.println(Thread.currentThread().getName());
        });*/

        Runnable runnable = ()->{
            synchronized (object){
                for (int i = 0 ;i<=10;i++){
                    if(object.flg){
                        System.out.println("one========"+Thread.currentThread().getName()+"++++++++"+i);
                        object.notify();
                        object.flg =false;
                    }else {
                        try {
                            object.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                }
            }
        };

        Runnable runnableW = ()->{
            synchronized (object){
                for (int i = 0 ;i<=10;i++){
                    if(!object.flg){
                        System.out.println("two==========="+Thread.currentThread().getName()+"++++++++"+i);
                        object.notify();
                        object.flg =true;
                    }else {
                        try {
                            object.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                }
            }

        };
        //将线程放入线程池中并启动
        /*new Thread(runnable).start();
        new Thread(runnableW).start();*/

       // pools.execute(runnable);
      //  pools.execute(runnableW);

        Runnable runnable3 = ()->{
            synchronized (ThreadPools.class){
                for (int i = 0 ;i<=10;i++){
                    if(i%2==0){
                        System.out.println(Thread.currentThread().getName()+"++++++++"+i);
                        ThreadPools.class.notify();

                    }else {
                        try {
                            ThreadPools.class.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                }
            }
        };

        Runnable runnable4 = ()->{
            synchronized (ThreadPools.class){
                for (int i = 0 ;i<=10;i++){
                    if(i%2==0){
                        System.out.println(Thread.currentThread().getName()+"++++++++"+i);
                        ThreadPools.class.notify();

                    }else {
                        try {
                            ThreadPools.class.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                }
            }
        };

        pools.execute(runnable3);
        pools.execute(runnable4);
    }
}
